package com.yg.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yg.config.result.R;
import com.yg.config.result.ResultCode;
import com.yg.entity.readAndWriteTemplates.QualityTemplate;
import com.yg.entity.singleEntity.*;
import com.yg.service.*;
import com.yg.service.impl.QualityCourseServiceImpl;
import com.yg.utils.DateValidationUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QualityReadListener extends AnalysisEventListener<QualityTemplate> {
    private TeacherService teacherService;
    private QualityCourseServiceImpl qualityCourseService;
    private QualityProfessionalService qualityProfessionalService;
    private QualityTeacherService qualityTeacherService;
    private QualityTeamService qualityTeamService;
    private QualityTitleService qualityTitleService;
    private R r;

    public QualityReadListener(TeacherService teacherService, QualityCourseServiceImpl qualityCourseService, QualityProfessionalService qualityProfessionalService, QualityTeacherService qualityTeacherService, QualityTeamService qualityTeamService, QualityTitleService qualityTitleService, R r) {
        this.teacherService = teacherService;
        this.qualityCourseService = qualityCourseService;
        this.qualityProfessionalService = qualityProfessionalService;
        this.qualityTeacherService = qualityTeacherService;
        this.qualityTeamService = qualityTeamService;
        this.qualityTitleService = qualityTitleService;
        this.r = r;
    }

    //表格数据列表
    private List<QualityTemplate> list = new ArrayList<>();
    //错误列表
    private List<String> errorList = new ArrayList<>();

    @Override
    public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {
        System.out.println("表头：" + headMap);
    }

    @Override
    public void invoke(QualityTemplate qualityTemplate, AnalysisContext analysisContext) {
        list.add(qualityTemplate);
    }

    /**
     * 这里注意了，要先去查询字典表，然后手动转为String，否则会抛出异常
     *
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        List<QualityCourse> qualityCourseList = new ArrayList<>();
        List<QualityProfessional> qualityProfessionalList = new ArrayList<>();
        List<QualityTeacher> qualityTeacherList = new ArrayList<>();
        List<QualityTeam> qualityTeamList = new ArrayList<>();
        List<QualityTitle> qualityTitleList = new ArrayList<>();


        list.forEach(e -> {

            /*--------------------省级以上精品课程（含省级）--------------------*/
            String excellentCoursesAboveTheProvincialLevelName = e.getExcellentCoursesAboveTheProvincialLevelName();
            String excellentCoursesAboveTheProvincialLevelTime = DateValidationUtil.dateValidation(e.getExcellentCoursesAboveTheProvincialLevelTime(),"yyyy.MM");
            QualityCourse qualityCourse = new QualityCourse();
            qualityCourse.setLevel("省级精品");
            qualityCourse.setName(excellentCoursesAboveTheProvincialLevelName);
            qualityCourse.setTime(excellentCoursesAboveTheProvincialLevelTime);
            qualityCourseList.add(qualityCourse);



            /*--------------------省级以上一流课程（含省级）--------------------*/
            String firstClassCoursesAtProvincialLevelOrAboveName = e.getFirstClassCoursesAtProvincialLevelOrAboveName();
            String firstClassCoursesAtProvincialLevelOrAboveTime = DateValidationUtil.dateValidation(e.getFirstClassCoursesAtProvincialLevelOrAboveTime(),"yyyy.MM");
            QualityCourse qualityCourse1 = new QualityCourse();
            qualityCourse1.setLevel("省级一流");
            qualityCourse1.setName(firstClassCoursesAtProvincialLevelOrAboveName);
            qualityCourse1.setTime(firstClassCoursesAtProvincialLevelOrAboveTime);
            qualityCourseList.add(qualityCourse1);

            /*--------------------校级在线开放课程--------------------*/
            String universityLevelOpenOnlineCoursesName = e.getUniversityLevelOpenOnlineCoursesName();
            String universityLevelOpenOnlineCoursesTime = DateValidationUtil.dateValidation(e.getUniversityLevelOpenOnlineCoursesTime(),"yyyy.MM");
            QualityCourse qualityCourse2 = new QualityCourse();
            qualityCourse2.setLevel("校级");
            qualityCourse2.setName(universityLevelOpenOnlineCoursesName);
            qualityCourse2.setTime(universityLevelOpenOnlineCoursesTime);
            qualityCourseList.add(qualityCourse2);


            /*--------------------省级以上特色专业（含省级）--------------------*/
            String specialtyAtProvincialLevelOrAboveName = e.getSpecialtyAtProvincialLevelOrAboveName();
            String specialtyAtProvincialLevelOrAboveTime = DateValidationUtil.dateValidation(e.getSpecialtyAtProvincialLevelOrAboveTime(),"yyyy.MM");
            QualityProfessional qualityProfessional = new QualityProfessional();
            qualityProfessional.setLevel("省级");
            qualityProfessional.setName(specialtyAtProvincialLevelOrAboveName);
            qualityProfessional.setTime(specialtyAtProvincialLevelOrAboveTime);
            qualityProfessionalList.add(qualityProfessional);

            /*--------------------省级以上一流专业（含省级）--------------------*/
            String firstClassMajorAtProvincialLevelOrAboveName = e.getFirstClassMajorAtProvincialLevelOrAboveName();
            String firstClassMajorAtProvincialLevelOrAboveTime = DateValidationUtil.dateValidation(e.getFirstClassMajorAtProvincialLevelOrAboveTime(),"yyyy.MM");
            QualityProfessional qualityProfessional1 = new QualityProfessional();
            qualityProfessional1.setLevel("省级一流");
            qualityProfessional1.setName(firstClassMajorAtProvincialLevelOrAboveName);
            qualityProfessional1.setTime(firstClassMajorAtProvincialLevelOrAboveTime);
            qualityProfessionalList.add(qualityProfessional1);


            /*--------------------省级以上教学名师（含省级）--------------------*/
            String famousTeachingTeachersAboveTheProvincialLevelName = e.getFamousTeachingTeachersAboveTheProvincialLevelName();
            String famousTeachingTeachersAboveTheProvincialLevelTime = DateValidationUtil.dateValidation(e.getFamousTeachingTeachersAboveTheProvincialLevelTime(),"yyyy.MM");
            QueryWrapper<Teacher> wrapper1 = null;

            QualityTeacher qualityTeacher = new QualityTeacher();
            qualityTeacher.setName(famousTeachingTeachersAboveTheProvincialLevelName);
            qualityTeacher.setTime(famousTeachingTeachersAboveTheProvincialLevelTime);
            qualityTeacherList.add(qualityTeacher);


            /*--------------------省级以上教学团队（含省级）--------------------*/
            String teachingTeamAboveTheProvincialLevelName = e.getTeachingTeamAboveTheProvincialLevelName();
            String teachingTeamAboveTheProvincialLevelTime = DateValidationUtil.dateValidation(e.getTeachingTeamAboveTheProvincialLevelTime(),"yyyy.MM");
            QualityTeam qualityTeam = new QualityTeam();
            qualityTeam.setName(teachingTeamAboveTheProvincialLevelName);
            qualityTeam.setTime(teachingTeamAboveTheProvincialLevelTime);
            qualityTeam.setLevel("省级");
            qualityTeamList.add(qualityTeam);

            /*--------------------校级教学团队--------------------*/
            String universityLevelTeachingTeamName = e.getUniversityLevelTeachingTeamName();
            String universityLevelTeachingTeamTime = DateValidationUtil.dateValidation(e.getUniversityLevelTeachingTeamTime(),"yyyy.MM");
            QualityTeam qualityTeam1 = new QualityTeam();
            qualityTeam1.setName(universityLevelTeachingTeamName);
            qualityTeam1.setTime(universityLevelTeachingTeamTime);
            qualityTeam1.setLevel("校级");
            qualityTeamList.add(qualityTeam1);


            QualityTitle qualityTitle = new QualityTitle();
            /*--------------------校级教学称号（含教学名师、卓越教学奖、教学能手等）--------------------*/
            String universityLevelTeachingTitleName = e.getUniversityLevelTeachingTitleName();
            String universityLevelTeachingTitleTitle = e.getUniversityLevelTeachingTitleTitle();
            String universityLevelTeachingTitleTime = DateValidationUtil.dateValidation(e.getUniversityLevelTeachingTitleTime(),"yyyy.MM");

            QualityTitle qualityTitle1 = new QualityTitle();
            qualityTitle1.setName(universityLevelTeachingTitleName);
            qualityTitle1.setTitle(universityLevelTeachingTitleTitle);
            qualityTitle1.setTime(universityLevelTeachingTitleTime);
            qualityTitleList.add(qualityTitle1);
        });

        //假如没有报错，则将数据存入到数据库中
        if (errorList.isEmpty()) {
            //存入到数据库中
            qualityCourseService.saveBatch(qualityCourseList);
            r.data("qualityCourseList", qualityCourseList.toString());

            qualityProfessionalService.saveBatch(qualityProfessionalList);
            r.data("qualityProfessionalList", qualityProfessionalList.toString());

            qualityTeacherService.saveBatch(qualityTeacherList);
            r.data("qualityTeacherList", qualityTeacherList.toString());

            qualityTeamService.saveBatch(qualityTeamList);
            r.data("qualityTeamList", qualityTeamList.toString());

            qualityTitleService.saveBatch(qualityTitleList);
            r.data("qualityTitleList", qualityTitleList.toString());
        } else {
            r.code(ResultCode.ERROR);
            r.success(false);
            r.message(errorList.toString());
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        System.out.println("错误：" + exception.getMessage());
        errorList.add("错误：" + exception.getMessage());
    }
}
